package com.lmm.lcuser.lc_user;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.lmm.dresswisdom.lmmframe.components.alert.AlertUtils;
import com.lmm.dresswisdom.lmmframe.util.LogUtils;
import com.lmm.lcuser.lc_user.pages.ShopMapActivity;
import com.lmm.lcuser.lc_user.service.UpdateIntentService;

import java.util.Timer;
import java.util.TimerTask;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * Created by xmgong on 2018/4/9.
 */

public class InitActivity extends AppBaseActivity {

    String[] needPermissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION};

    private int time = 3;
    Handler mhandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 1) {
                time--;
                if (time == 0) {
                    toMain();
                }
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
        hideActionBar();
        PermissionGen.needPermission(this, 100, needPermissions);
        startUpdateService();
    }


    @PermissionSuccess(requestCode = 100)
    private void doSuccess() {
        LogUtils.write("授权成功");
        start();
    }

    private void start() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mhandler.sendEmptyMessage(1);
            }
        }, 100, 1000);
    }


    @PermissionFail(requestCode = 100)
    private void doFaile() {
        LogUtils.write("PermissionFail");
        AlertUtils.remindAlert(this, "提醒", "部分权限未授权,App部分功能会受影响", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                toMain();
            }
        });
    }


    private void toMain() {
//        startActivity(new Intent(InitActivity.this, ShopMapActivity.class));
        startActivity(new Intent(InitActivity.this, MainActivity.class));
        finish();
    }

    private void startUpdateService() {
        Intent intent = new Intent(this, UpdateIntentService.class);
        startService(intent);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}
